package com.yeqiangwei.commons.net.httpclient;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
import org.apache.http.util.EntityUtils;

public class InputStreamResponseHandler implements
		org.apache.http.client.ResponseHandler<InputStream> {

	@Override
	public InputStream handleResponse(HttpResponse response)
			throws ClientProtocolException, IOException {
		StatusLine statusLine = response.getStatusLine();
		if (statusLine.getStatusCode() >= 300) {
			throw new HttpResponseException(statusLine.getStatusCode(),
					statusLine.getReasonPhrase());
		}
		HttpEntity entity = response.getEntity();
		if(entity == null){
			return null;
		}
		byte[] bytes = EntityUtils.toByteArray(entity);
		return new ByteArrayInputStream(bytes);
	}

}
